//布局设置
import objectPath from "object-path";
import merge from "deepmerge";
//读取布局设置 图片 颜色等配置数据
import config from "@/core/config/layout.config.json";
//设置布局配置
export const SET_LAYOUT_CONFIG = "setLayoutConfig";
//重置布局配置
export const RESET_LAYOUT_CONFIG = "resetLayoutConfig";
//覆盖布局配置
export const OVERRIDE_LAYOUT_CONFIG = "overrideLayoutConfig";
//替代页面布局配置
export const OVERRIDE_PAGE_LAYOUT_CONFIG = "overridePageLayoutConfig";
//可改变类型
export default {
    state: {
        config: config,
        initial: config
    },
    getters: {
        /**
         * 从布局配置中获取配置
         * @param state
         * @returns {function(path, defaultValue): *}
         */
        layoutConfig: state => (path, defaultValue) => {
            return objectPath.get(state.config, path, defaultValue);
        }
    },
    actions: {
        /**
         * 设置并替换整个配置
         * @param state
         * @param payload
         */
        [SET_LAYOUT_CONFIG](state, payload) {
            state.commit(SET_LAYOUT_CONFIG, payload);
        },
        /**
         * 将配置重置为初始状态
         * @param state
         */
        [RESET_LAYOUT_CONFIG](state) {
            state.commit(RESET_LAYOUT_CONFIG);
        },
        /**
         *使用布局生成器在缓存中保存的配置重置配置
         * @param state
         */
        [OVERRIDE_LAYOUT_CONFIG](state) {
            state.commit(OVERRIDE_LAYOUT_CONFIG);
        },
        /**
         * 按页面级别覆盖配置
         * @param state
         * @param payload
         */
        [OVERRIDE_PAGE_LAYOUT_CONFIG](state, payload) {
            state.commit(OVERRIDE_PAGE_LAYOUT_CONFIG, payload);
        }
    },
    mutations: {
        [SET_LAYOUT_CONFIG](state, payload) {
            state.config = payload;
        },
        [RESET_LAYOUT_CONFIG](state) {
            state.config = Object.assign({}, state.initial);
        },
        [OVERRIDE_LAYOUT_CONFIG](state) {
            state.config = state.initial = Object.assign(
                {},
                state.initial,
                JSON.parse(localStorage.getItem("config"))
            );
        },
        [OVERRIDE_PAGE_LAYOUT_CONFIG](state, payload) {
            state.config = merge(state.config, payload);
        }
    }
};
